package com.annoncesautomobile.modele.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.annoncesautomobile.vue.utils.AttributValeur;

import android.os.Parcel;
import android.os.Parcelable;

public class Requete implements Parcelable{
	private Map<String,AttributValeur> myMap;

	public String getParameters()
	{
		StringBuilder sb = new StringBuilder();
		for(Entry<String, AttributValeur> aValue : this.myMap.entrySet())
		{
			if(aValue.getValue() != null)
			{
				sb.append("&"+aValue.getKey()+"="+aValue.getValue().getAttribut());
			}
		}
		return sb.toString();
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeInt(this.myMap.size());
		for(String key : this.myMap.keySet()){
			out.writeString(key);
			out.writeString(this.myMap.get(key).getAttribut());
			out.writeString(this.myMap.get(key).getValeur());
		}
	}

	public void put(String key, String value, String value2Display)
	{
		this.myMap.put(key, new AttributValeur(value, value2Display));
	}
	public void put(String key, String value)
	{
		put(key,value,value);
	}

	public String get(String key)
	{
		if(this.myMap.containsKey(key))
		{
			return this.myMap.get(key).getAttribut();
		}
		else
		{
			return null;
		}
	}

	public String getDisplay(String key)
	{
		if(this.myMap.containsKey(key))
		{
			return this.myMap.get(key).getValeur();
		}
		else
		{
			return null;
		}
	}

	public void remove(String key)
	{
		this.myMap.remove(key);
	}

	public Requete(Parcel in)
	{
		this.myMap = new HashMap<String, AttributValeur>();
		int size = in.readInt();
		for(int i = 0; i < size; i++){
			String key = in.readString();
			String value = in.readString();
			String value2Display = in.readString();
			this.myMap.put(key,new AttributValeur(value, value2Display));
		}
	}

	public Requete() {
		this.myMap = new HashMap<String, AttributValeur>();
	}

	public static final Parcelable.Creator<Requete> CREATOR= new Parcelable.Creator<Requete>() {

		@Override
		public Requete createFromParcel(Parcel source) {
			return new Requete(source);
		}

		@Override
		public Requete[] newArray(int size) {
			return new Requete[size];
		}
	};

	public Map<String, String> getMapMode() {
		Map<String,String> returMap = new HashMap<String, String>();
		for(Entry<String, AttributValeur> aEntry: this.myMap.entrySet())
		{
			returMap.put(aEntry.getKey(), aEntry.getValue().getAttribut());
		}
		return returMap;
	}

	public boolean isAlerteFromFormAnnonce(boolean deep)
	{
		List<String> mandatoryFields = new ArrayList<String>();
		mandatoryFields.add("marque");
		mandatoryFields.add("serie");
		if(deep)
		{
			mandatoryFields.add("email_autotheque");
		}
		//mandatoryFields.add("categorie");
		//mandatoryFields.add("prix_max");
		//mandatoryFields.add("energie");
		//mandatoryFields.add("km");
		//mandatoryFields.add("annee_max");

		for(String s : mandatoryFields)
		{
			if(!myMap.containsKey(s))
			{
				return false;
			}
		}
		return true;
	}

	public HashMap<String,String> getAlerteFromFormAnnonce()
	{
		//PRAGMA : La requete est convertible
		HashMap<String,String> r = new HashMap<String, String>();
		r.put("marque", get("marque"));
		r.put("serie", get("serie"));
		if(get("categorie") != null)
		{
			r.put("categorie", get("categorie"));
		}
		if(get("prix_max") != null)
		{
			r.put("budget", get("prix_max"));
		}
		if(get("energie") != null)
		{
			r.put("energie", get("energie"));
		}
		if(get("km") != null)
		{
			r.put("km", get("km"));
		}
		if(get("annee_min") != null)
		{
			r.put("annee", get("annee_min"));
		}
		return r;
	}

	public boolean equals(Object aThat)
	{
		if ( this == aThat ) return true;
		if ( !(aThat instanceof Requete) ) return false;
		Requete that = (Requete)aThat;
		for(Entry<String,String> aEntry : this.getMapMode().entrySet())
		{
			String key = aEntry.getKey();
			String value = aEntry.getValue();
			if(that.get(key) == null || that.get(key).equals(value))
			{
				return false;
			}
		}
		for(Entry<String,String> aEntry : that.getMapMode().entrySet())
		{
			String key = aEntry.getKey();
			String value = aEntry.getValue();
			if(this.get(key) == null || this.get(key).equals(value))
			{
				return false;
			}
		}
		return true;
	}
}
